$(function () {
    //获取url
    var url = localStorage.getItem("ajaxUrl");
    var pass = 1;
    var focus = true;

    $(".head .refresh").on("click", function () {
        window.location.href = "acid.html";
    });

    $(".head .icon-lishi1").on("click", function () {
        localStorage.setItem("acidhtml", "acid");
        window.location.href = "acid_lishi.html";
    });

    //搜索框 获取焦点
    $("#goods_id").focus();
    /*$("#goods_id").blur(function () {
        if (focus) {
            $("#goods_id").focus();
        }
    });*/

    $("div.page").on("click", "span.personal", function() {
        localStorage.removeItem("packMachine");
        focus = false;
        $("#goods_id").blur();
    });

    $(".panel-overlay").on("click", function () {
        focus = true;
        setTimeout(function () {
            $("#goods_id").val("").focus();
        }, 1000);
    });


    getPlan();
    //根据扫描内容 得到 产品信息
    $("#goods_id").on("keydown", function (event) {
        if (event.keyCode == "13") {
            if (pass == 0) {
                return
            }
            pass = 0;
            var val = $(this).val();
            if (!val) {
                $alert("请扫描排产单号！");
                setTimeout(function () {
                    pass = 1;
                    $("#goods_id").val("").focus();
                }, 1400);
                return;
            }
            $(this).val("");
           getPlan(val);
        }
    });

    function getPlan(workplanNumber) {
        loading_show("获取中...");
        $.ajax({
            headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
            type: "post",
            url: url + '/app/acid/getAcidtaskplandetails',
            data: {
                workplanNumber: workplanNumber,
            },
            dataType: 'json',
            json: 'callback',
            cache: false,
            success: function (res) {
                pass = 1;
                loading_hide();
                if (res.success) {
                    var data = res.result;
                    $(".content .list-block .goodsList_ul").empty();
                    for (var i = 0; i < data.length; i++) {
                        var data_html = '<li><div class="row no-gutter">' +
                            '<div class="col-100">' +
                            '<p class="p_none"><span class="id">' + data[i]._id + '</span></p>' +
                            '<p><span>排产单号:</span><span class="sp goods_pcdh">' +
                            data[i].plan.workplanNumber + '</span></p><p><span>订单号:</span><span class="sp goods_cpddh">' +
                            data[i].plan.saleorderNumber + '</span><span>产线:</span><span class="sp goods_cpcx">' +
                            (data[i].workCenterName || "空") + '</span></p><p><span>订单类型:</span><span class="sp goods_cpkhmc">' +
                            data[i].plan.saleordertype + '</span></p><p><span>客户名称:</span><span class="sp goods_cpkhmc">' +
                            data[i].plan.customerName + '</span></p><p><span>钢号:</span><span class="sp goods_cpgh">' +
                            data[i].inventorySteel + '</span><span>规格:</span><span class="sp goods_cpgg">' +
                            data[i].inventorySpecification + '</span></p><p><span>计划件数:</span><span class="sp goods_cpjhl">' +
                            data[i].root + '</span><span>完成件数:</span><span class="sp goods_cpwcl">' +
                            (data[i].completeRoot || "0") + '</span></p><p><span>计划开始时间:</span><span class="sp goods_cpkssj">' +
                            data[i].startTime + '</span></p><p><span>计划接收时间:</span><span class="sp goods_cpjssj">' +
                            data[i].endTime + '</span></p></div>' +
                            '</div></li>';
                        $(".content .list-block .goodsList_ul").append(data_html);
                    }
                    $alert("获取成功", 800);
                    badge_text();
                } else {
                    setTimeout(function () {
                        $("#goods_id").val("").focus();
                    }, 1400);
                    $alert(res.message);
                }
            },
            error: function (error) {
                pass = 1;
                loading_hide();
                console.log(error);
                setTimeout(function () {
                    $("#goods_id").val("").focus();
                }, 1400);
                $alert(error.responseText);
            }
        });
    }

    // li 中的 减号 点击 删除 本条
    $(".content").on("click", ".goodsList_ul li", function () {
        var jianhao = $(this);
        var id = jianhao.find("span.id").text();
        var pcdh = jianhao.find("span.goods_pcdh").text();
        $.confirm('确定生产' + pcdh + '计划吗？', function () {
            localStorage.setItem("work", JSON.stringify({id: id, pcdh: pcdh}));
            window.location.href = 'acid_one.html';
        });
    });

    //点击 某一条 数据  改变 背景颜色
    $(document).on("click", ".content .goodsList_ul li", function () {
        $(this).css("background", "#cccccc").siblings("li").css("background", "#ffffff");
    });

    // goodsList中 badge 的值 为 列表中 li 的个数
    function badge_text() {
        var badge_len = $(".content .list-block .goodsList_ul li").length;
        // console.log(badge_len);
        $(".ul_foot span.sp_js").html(badge_len);
    }

    badge_text();

});